home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / prog / cfuncs.zip / WEDIT.C < prev    next >
C/C++ Source or Header  |  1991-12-20  |  7KB  |  267 lines

  1. #include <conio.h>
  2. #include <alloc.h>
  3. #include <stdio.h>
  4. #include <funcs.h>
  5.  
  6. unsigned Max = 1000;
  7.  
  8. /*----------------------------------------------------------------*/
  9. /*---------------------------wedit--------------------------------*/
  10. /*            WINDOW EDIT                  */
  11. /*                                  */
  12. /*DESCRIPTION: Creates a window for data entry with an option to  */
  13. /*    check each line with CheckNum.  Writes the data to a file */
  14. /*                                  */
  15. /*INPUT:                                                          */
  16. /*    filename - name for output file                             */
  17. /*    width    - width of the window                              */
  18. /*    length   - length of the window                             */
  19. /*    fcheck  - a function that performs a check on the          */
  20. /*          entries.  Returns 1 for a pass, 0 for fail.      */
  21. /*          Passing NULL means no check will be performed.  */
  22. /*USES: Frame, strspc, OnCursor, OffCursor              */
  23. /*----------------------------------------------------------------*/
  24.  
  25. wedit( const char *filename, int width, int length,
  26.     int (*fcheck) (const char *))
  27.  
  28. {
  29.  
  30.   int i, key = '', move = 1, done = 1, LinMod;
  31.   int x, y, cursorx = 0, cursory = 0, index = 0, count = 0;
  32.   int TopWinElt = 0, OldWin= 3, movey = 0, movex = 0, tmp;
  33.   FILE *fp;
  34.   char *list;
  35.   char buffer[26], action;
  36.   FrameDataType Fr;
  37.   void DisplayWeditWindow(char *list, int TopOfWin, int X, int Y, int W,
  38.             int Length, int Count);
  39.   void BuildWeditScreen();
  40.  
  41. /* end data declarations */
  42.  
  43.   action = AddAppend(filename);
  44.   if (action == 'X') return(0);
  45.  
  46.   y = 14 - (length/2);
  47.   x = 36 - (width/2);
  48.  
  49.   /*------------ALLOCATE MEMORY---------*/
  50.   if((list = (char *) calloc(Max, width+1))==NULL)
  51.   {
  52.      message(0, " Not Enough Memory For Function. ");
  53.      return(0);
  54.   }
  55.  
  56.  /*-------------READ IN DATA FROM FILE------------*/
  57.   if (action != 'O' && action != 'A')
  58.     action = 'A';
  59.   if (action == 'A')
  60.     {
  61.     if ((fp = fopen(filename, "rt")) != NULL)
  62.       {
  63.      while (fgets(buffer, 26, fp) != NULL)
  64.         memcpy(list+(count++)*(width+1), buffer, width);
  65.      TopWinElt = (count/length) * length;
  66.      cursory = count % length;
  67.      index = TopWinElt + cursory;
  68.       }
  69.       else  /* file not found */
  70.      action = 'O';
  71.     }  /* append */
  72.  
  73.   BuildWeditScreen();
  74.  
  75.   /*-------------DISPLAY FRAME-------------*/
  76.   Fr.clear = 2;
  77.   Frame(&Fr);
  78.  
  79.   Fr.X = x - 1; Fr.Y = y - 1;
  80.   Fr.L = length +2; Fr.W = width +2;
  81.   Fr.F = CYAN; Fr.B = BLACK;
  82.   Fr.BorderType = 0;
  83.   Frame(&Fr);
  84.   textattr(CYAN);
  85.   OnCursor();
  86.  
  87.   /*-------------MAIN LOOP----------------*/
  88.   while (done != 0)
  89.   {
  90.   /*-------------DISPLAY STATS------------*/
  91.     if (move != 0)
  92.     {
  93.       WriteAt(20, 15, YELLOW, BLACK, "%-4d", index+1);
  94.  
  95.       WriteAt(20, 16, YELLOW, BLACK, "%-4d", count );
  96.  
  97.       WriteAt(20, 17, YELLOW, BLACK, "%-4d", Max-count );
  98.  
  99.       move = 0;
  100.     }
  101.  
  102.   /*-------------DISPLAY WINDOW-----------*/
  103.     if(OldWin != TopWinElt)
  104.         DisplayWeditWindow(list, TopWinElt, x, y, width, length, count);
  105.     gotoxy(x + cursorx, y+cursory);
  106.     OldWin = TopWinElt;
  107.  
  108.  
  109.   /*-------------GET KEY STROKE------------*/
  110.     key = getch();
  111.  
  112.     if (key == '\0')
  113.       key = 256 + getch();
  114.     else if (key >=32 && key <= 256)
  115.     {
  116.     putch(key);
  117.     *(list+index*(width+1) + cursorx) = key;
  118.     movex++;
  119.     }
  120.  
  121.     switch (key){
  122.     case 324:   /* F10 asc 0, 68         */
  123.           done=0;
  124.           break;
  125.     case 328:   /* Up arrow asc 0, 72    */
  126.       --movey;
  127.           break;
  128.     case 336:   /* Down arrow asc 0, 80  */
  129.       movey++;
  130.           break;
  131.     case 333:   /* Right arrow asc 0, 77 */
  132.       movex++;
  133.           break;
  134.     case 331:   /* Left arrow asc 0, 75  */
  135.       movex--;
  136.           break;
  137.         case 13:   /* Enter asc 13       */
  138.       movex = -cursorx;
  139.       movey++;
  140.           break;
  141.     case 327:   /* Home asc 0, 71        */
  142.       movey = -index;
  143.       movex = -cursorx;
  144.           break;
  145.     case 335:   /* End Key asc 0, 79        */
  146.       movey = count - index -1;
  147.       movex = -cursorx;
  148.           break;
  149.     case 329:   /* Pageup Key asc 0, 73        */
  150.       movey -= length;
  151.           break;
  152.     case 337:   /* Page down Key asc 0, 81        */
  153.       movey += length;
  154.           break;
  155.     case  8:   /* BackSpace asc 8    */
  156.       if(cursorx != 0) {
  157.          gotoxy(x+cursorx-1, y+cursory);
  158.          for(i = cursorx-1; i< width-1; ++i) {
  159.         *(list+index*(width+1)+i) = *(list+index*(width+1)+i+1);
  160.         putch(*(list+index*(width+1)+i)); }
  161.          *(list+index*(width+1)+i) = ' ';
  162.          putch(' ');
  163.          movex--; }
  164.       break;
  165.     case 339:  /*  Del key  asc 0, 83*/
  166.         for(i = cursorx; i< width -1; ++i) {
  167.            *(list+index*(width+1)+i) = *(list+index*(width+1)+i+1);
  168.            putch(*(list+index*(width+1)+i)); }
  169.         *(list+index*(width+1)+i) = ' ';
  170.         putch(' ');
  171.       break;
  172.         default:
  173.           break;
  174.     } /* end switch */
  175.  
  176.  
  177. /*-----------PROCESS KEY STROKE-----------*/
  178.     if(movex + cursorx > width - 1) {
  179.        movex = -cursorx;
  180.        movey++; }
  181.     else if(movex + cursorx < 0) {
  182.        movex = width - cursorx -1;
  183.        movey--; }
  184.  
  185. /*------------ CHECK FOR A VALID NUMBER----------*/
  186.    if (fcheck != NULL)
  187.     {
  188.        if (key >= 48 && key <= 125)  LinMod = 1;
  189.        if (LinMod && movey)
  190.        {   if( !CkBeep(fcheck(list+index*(width+1))))
  191.        {   movex = 0;
  192.            movey = 0;
  193.        }
  194.        LinMod = 0;
  195.        }
  196.     }
  197.  
  198.     if(movey + cursory > length-1)
  199.     {   /* past bottom of window */
  200.        if((tmp = (index + movey) / length * length ) < Max)
  201.        {
  202.         TopWinElt = tmp;
  203.         movey = -(cursory - (cursory+movey) % length);
  204.        }
  205.     }
  206.     else if(movey + cursory < 0)    /* past top of window */
  207.        if(index + movey >= 0) {      /* check for top of list */
  208.       TopWinElt = (index + movey)/length *length;
  209.       movey = -(cursory - (index + movey) % length); }
  210.        else
  211.       movey = 0;
  212.  
  213.     if(index +1 > count && !blanks(list+index*(width+1))) {
  214.        count = index +1;
  215.        ++move; }
  216.  
  217.     cursorx += movex;
  218.     if(TopWinElt+cursory+movey< Max)
  219.     cursory += movey;
  220.     else /* tried to go past max limit */
  221.     {
  222.     Beep(250, 2); delay(100);
  223.     Beep(250, 2); delay(100);
  224.     Beep(250, 2);
  225.     }
  226.  
  227.     if(movey || OldWin != TopWinElt) ++move;
  228.     movex = movey = 0;
  229.  
  230.     index = TopWinElt + cursory;
  231.  
  232.   } /* end while */
  233.  
  234.  /*-------------WRITE TO THE FILE-------------*/
  235.   if ((fp = fopen(filename, "wt")) != NULL)
  236.     for (i=0; i<count; i++)
  237.       if(!blanks(list+i*(width+1)))
  238.     fprintf( fp, "%s\n", list+i*(width+1));
  239.  
  240.   fclose( fp );
  241.   free(list);
  242.  
  243.   clrscr();
  244.   return(1);
  245.  
  246. }  /* end wedit */
  247.  
  248. void DisplayWeditWindow(char *list, int TopOfWin, int X, int Y, int W,
  249.             int Length, int Count)
  250. {
  251.  
  252.   int i;
  253.  
  254.   if (TopOfWin < 0) TopOfWin = 0;
  255.  
  256.   for (i = TopOfWin; i < TopOfWin + Length; i++)
  257.       if(i < Count)   /* output data */
  258.     WriteAt(X, Y+i-TopOfWin, CYAN, BLACK, list+i*(W+1));
  259.       else     /* past entries, so print blanks */
  260.     WriteAt(X, Y+i-TopOfWin, CYAN, BLACK, "%*s", W, " ");
  261. }
  262. /*
  263. void main()
  264. {
  265.    wedit("invite.dat", 9, 10, CheckNum);
  266. }
  267. */